// AWTLabel.cpp: implementation of the AWTLabel class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "AWT.h"
#include "AWTLabel.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

IMPLEMENT_DYNAMIC(AWTLabel, AWTComponent)

AWTLabel::AWTLabel()
{
	construct("", LEFT);
}

AWTLabel::AWTLabel(LPTSTR text)
{
	construct(CString(text), LEFT);
}

AWTLabel::AWTLabel(CString text)
{
	construct(text, LEFT);
}

AWTLabel::AWTLabel(CString* text)
{
	construct(*text, LEFT);
}

AWTLabel::AWTLabel(CString text, int align)
{
	construct(text, align);
}

AWTLabel::~AWTLabel()
{
}

CWnd* AWTLabel::createComponent()
{
	CWnd* wParent = getParent()->getPeer();
	if (wParent == NULL)
		return wParent;
	CStatic* pwnd = new AWTLabelPeer(this);
	CWnd* wndParent = getParent()->getPeer();
	if (wndParent == NULL)
		return NULL;
	pwnd->Create(m_text, WS_CHILD | WS_VISIBLE, getBounds(), wParent);
	pwnd->SetFont(CFont::FromHandle(m_font));
	if (!m_enabled)
		pwnd->EnableWindow(FALSE);
	return pwnd;
}

CSize AWTLabel::getMinimumSize()
{
	return getPreferredSize();
}

CSize AWTLabel::getPreferredSize()
{
	HDC hdc = ::GetDC(NULL);
	CString s;
	s = getText();
	CDC* pdc = CDC::FromHandle(hdc);
	CFont* ft = CFont::FromHandle(getFont());
	CFont* ftOld = pdc->SelectObject(ft);
	if (s.IsEmpty())
		s = "X";
	CSize sz = pdc->GetTextExtent(s);
	pdc->SelectObject(ftOld);
	::ReleaseDC(NULL, hdc);	
	sz.cx += 10;
	return sz;
}

